package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.CommonBackpack;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameBackpack;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author : luoyong
 * @date : 2020-07-01 16:22
 **/
@Component(PtoNum.SWEAPONRYRESPONSE + "")
public class SWeaponryResponse implements ReceiveMsg {
    private final static Logger logger = LoggerFactory.getLogger(SWeaponryResponse.class);
    private ClientHandler clientHandler = ClientHandler.getInstance();
    @Autowired
    private UserServiceCache userServiceCache;

    @Override
    public void execute(Message msg) {
        GameBackpack.SWeaponryResponse sWeaponryResponse = (GameBackpack.SWeaponryResponse) msg;
        int code = sWeaponryResponse.getCode();
        switch (code) {
            case CommonBackpack.WEAPONRY_SUCCESS:
                Map<String, GameEntity.Weaponry> weaponryMap = userServiceCache.getUserInMap().get(sWeaponryResponse.getUserId()).getWeaponryMap();
                weaponryMap.clear();
                sWeaponryResponse.getWeaponryList().forEach(weaponry -> weaponryMap.put(weaponry.getSelfWeaponryId(), weaponry));
                logger.info(sWeaponryResponse.getContent());
                break;
            case CommonBackpack.WEAPONRY_ERROR:
                logger.info(sWeaponryResponse.getContent());
                break;
            default:
                break;
        }
    }
}
